﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Modules.Guide.Runtime
{
    public class GuideFollow : MonoBehaviour
    {
        public Transform m_target;
        public Camera m_targetCamera;
        public Transform m_uiContainer;
        public Camera m_uiCamera;
        public Vector2 m_offset;
        public bool m_cameraLock;
    
        public bool CameraLock
        {
            get
            {
                return m_cameraLock;
            }
            set
            {
                m_cameraLock = value;
                WorldCameraManager.cameraController.SetCameraLockStatus(m_cameraLock);
            }
        }
    
        public void Follow(Transform target, Camera targetCamera, Transform uiContainer, Camera uiCamera, Vector2 offset)
        {
            m_target = target;
            m_targetCamera = targetCamera;
            m_uiContainer = uiContainer;
            m_uiCamera = uiCamera;
            m_offset = offset;
            
            Update();
        }
        
    
        // Update is called once per frame
        public void Update()
        {
            if (m_target)
            {
                Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(m_targetCamera, m_target.position);
                Vector2 localPoint;
                RectTransformUtility.ScreenPointToLocalPointInRectangle(m_uiContainer as RectTransform, screenPoint, m_uiCamera, out localPoint);
    
                (transform as RectTransform).anchoredPosition = localPoint + m_offset;
    
                if (m_cameraLock)
                {
                    WorldCameraManager.cameraController.SetCameraLockStatus(m_cameraLock);
                }
            }
        }
    
        public void Clear()
        {
            m_target = null;
            m_targetCamera = null;
            m_uiContainer = null;
            m_uiCamera = null;
        }
    }

}